package com.szty.libarytest.mediaProjection

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.media.projection.MediaProjectionManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.szty.libarytest.R
import com.szty.libarytest.webTest.toast
import kotlinx.android.synthetic.main.activity_screen_record.*

/**
 *@author : created by SunJH
 *时间：2021/7/26 002615
 *作用： 屏幕录制页面
 */
class ScreenRecordActivity : AppCompatActivity() {
    private val REQUEST_CODE = 1
    private val mMediaProjectionManager by lazy {
        getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
    }
    private val screenRecord by lazy {
        val screenRecordService = ScreenRecordUtils(this)
        screenRecordService
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_screen_record)
        checkPermission()
        btn_start.setOnClickListener {
            if (screenRecord.screenRecordStatus == ScreenRecordUtils.PAUSE) {
                screenRecord.resumeScreenRecord()
            } else {
                createScreenCapture()
            }
        }
        btn_stop.setOnClickListener {
            screenRecord.stopScreenRecord()
        }
        btn_pause.setOnClickListener {
            screenRecord.pauseScreenRecord()
        }
        screenRecord.callTime = {
            tv_time.text = it
        }
    }

    private fun createScreenCapture() {
        val createScreenCaptureIntent = mMediaProjectionManager.createScreenCaptureIntent()
        startActivityForResult(createScreenCaptureIntent, REQUEST_CODE)
    }

    fun checkPermission() {
        var requestPermissionResult =
            ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) +
                    ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) +
                    ContextCompat.checkSelfPermission(
                        this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                    ) +
                    ContextCompat.checkSelfPermission(
                        this,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                    )
        if (requestPermissionResult != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.RECORD_AUDIO
                ),
                123
            )
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            try {
                data?.putExtra("resultCode", resultCode)
                screenRecord.startScreenRecord(data!!)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        } else {
            "用户拒绝录屏".toast()
        }
    }

}



